<template>
	<view>
		<view class="">
			<view class="goods-section">
				<view class="g-header b-b">
					<text class="name">多米街拼团</text>
				</view>
				<!-- 商品列表 -->
				<view class="g-item">
					<image :src="pintuanInfo.img"></image>
					<view class="right">
						<text class="title clamp">{{pintuanInfo.name||'批量参团'}}</text>
						<view class="price-box">
							<text class="price">￥{{pintuanInfo.price}}</text>
							<text class="number">x 1</text>
						</view>
					</view>
				</view>
			</view>
			<view class="yt-list">
				<view class="yt-list-cell b-b">
					<text class="cell-tit clamp">
						共 {{pintuanInfo.num||1}} 件商品
					</text>
					<text class="cell-tip">
						合计：￥{{pintuanInfo.price}}
					</text>
					<text class="cell-more wanjia wanjia-gengduo-d"></text>
				</view>
			</view>
			<!-- 支付方式 -->
			<view class="yt-list">
				<view class="yt-list-cell b-b">
					<text class="cell-tit">是否使用米券<text
							style="color:#8405dc">（可用{{displayInfo.feemoney}}米券）</text></text>
					<switch color="#8405dc" @change="switch1Change" />
				</view>
			</view>
			<!-- 金额明细 -->
			<view class="yt-list">
				<view class="yt-list-cell b-b" v-if="isMi">
					<text class="cell-tit clamp">米券抵扣</text>
					<input type="number" class="text-right" v-model="mpay" @input="changeInput" placeholder="输入米券抵扣数量">
				</view>
				<view class="yt-list-cell b-b">
					<text class="cell-tit clamp">合计</text>
					<text class="cell-tip active">¥{{pintuanInfo.price}}</text>
				</view>

				<view class="yt-list-cell desc-cell">
					<text class="cell-tit clamp">备注</text>
					<input class="desc" type="text" v-model="remark" placeholder="请填写备注信息"
						placeholder-class="placeholder" />
				</view>
			</view>
		</view>
		<view class="c_info" v-if="displayInfo.issend==1">
			注意：运费默认为初始运费，超重和偏远地区需要补运费差价后才可以发货，请注意接听仓库客服电话通知。
		</view>
		<!-- 确定支付 -->
		<view class="footer">
			<view class="price-content">
				<view style="font-weight: bold;">
					<text style="font-size: 32upx;margin-right: 5upx;">实付款</text>
					<text class="price">{{amountPaid}}元</text>
				</view>
			</view>
			<text class="submit" :class="{disabledSumbit:disabledSumbit}" @click="!disabledSumbit&&submit()">创建订单</text>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	export default {
		computed: {
			amountPaid() {
				return ((this.pintuanInfo.price || 0) * 1000 - (this.mpay || 0) * 1000) / 1000
			}
		},
		data() {
			return {
				isMi: false, //是否使用米券支付
				mpay: '', //使用米券数量
				remark: '', //备注
				optionData: {}, //参数对象
				pintuanInfo: {}, //拼团商品
				displayInfo: {}, //订单信息
				isloading: false,
				mid: '',
				code: '',
				disabledSumbit: true,
			}
		},
		async onLoad(option) {
			this.optionData = option
			if (option.mid) {
				this.mid = option.mid;
			}
			if (option.code) {
				this.code = option.code
			}
			if (!uni.getStorageSync('token')) {
				return this.getToken();
			}
			this.getPinTuan(option.leader_member_id ? option.leader_member_id : '') //获取拼团数据
			this.getDisplay()
		},
		methods: {
			async getToken() {
				// #ifdef MP-WEIXIN
				uni.login({
					provider: 'weixin',
					success: res => {
						this.$axios('login/autologin', 'POST', 'member', {
							mid: this.mid,
							code: res.code,
							aid: '9184'
						}).then(res => {
							if (res.data.code == 200) {
								uni.setStorageSync('token', res.data.data);
								this.getPinTuan(this.optionData.leader_member_id ? this.optionData
									.leader_member_id : '') //获取拼团数据
								this.getDisplay()
							}
						})
					}
				})
				// #endif
				// #ifdef H5
				this.$axios('login/autoGzlogin', 'POST', 'member', {
					mid: this.mid,
					code: this.code,
				}).then(res => {
					if (res.data.code == 300001) {
						window.location.href = res.data.data;
					} else if (res.data.code == 200) {
						uni.setStorageSync('token', res.data.data);
						this.getPinTuan(this.optionData.leader_member_id ? this.optionData
							.leader_member_id : '') //获取拼团数据
						this.getDisplay()
					}
				})
				// #endif
				// #ifdef MP-ALIPAY
				uni.setStorageSync('route',this.getFullCurrentRoute())
				this.$navHome(`/pages/public/login?mid=${this.mid||''}`)
				// #endif
			},
			// 获取拼团数据
			async getPinTuan(leader_member_id) {
				this.disabledSumbit = true
				let url = 'Group/getJoinGroupPrice'
				if (leader_member_id) {
					url = 'Group/getJoinGroupPriceMulti'
				}
				if (this.optionData.yuyue == 1) {
					url = 'DateGroup/getJoinGroupPrice'
				}
				const res = await this.$axios(url, 'POST', 'pintuan', {
					pintuan_list_id: this.optionData.pintuan_list_id,
					leader_member_id,
					date_card_id: this.optionData.pintuan_card_id,
					...this.optionData
				})
				this.disabledSumbit = false
				const data = res.data.data
				if (res.data.code == 200) {
					if (leader_member_id || this.optionData.yuyue == 1) {
						this.pintuanInfo = res.data.data || {}
					} else {
						this.pintuanInfo = res.data.data.pintuan_card_info || {}
					}
					if (Boolean(data.pintuan_limit_msg)) {
						uni.showModal({
							title: '温馨提示',
							content: data.pintuan_limit_msg,
							success: () => {
								this.$navTo('/pages/index/home')
							}
						})
					}
				}
			},
			// 获取订单数据
			async getDisplay() {
				const res = await this.$axios('confirm/display', 'POST', 'order', this.optionData)
				if (res.data.code == 200) {
					this.displayInfo = res.data.data || {}
				}
			},
			// 限制米券输入为整数
			changeInput(e) {
				setTimeout(() => {
					let val = Number(e.detail.value.replace(/\D/g, ''))
					this.mpay = val > this.pintuanInfo.price ? Math.floor(this.pintuanInfo.price) : val <= 0 ? '' :
						val
				}, 0)
			},
			// 切换是否使用米券
			switch1Change(e) {
				this.isMi = e.detail.value
				if (this.isMi) {
					let feemoney = this.displayInfo.feemoney,
						price = this.pintuanInfo.price
					this.mpay = price > feemoney ? feemoney : Math.floor(price)
				} else {
					this.mpay = '';
				}
			},

			// 添加提交状态
			createLoading() {
				uni.showLoading({
					title: '提交中'
				})
				this.isloading = true;
			},
			// 提交中状态消除
			clearLoading(isShow = false, data = '') {
				this.isloading = false;
				uni.hideLoading()
				if (isShow) {
					uni.showModal({
						title: '提示',
						content: data,
						showCancel: false,
						success: function(res) {
							if (res.confirm) {}
						}
					});
				}
			},
			// 立即支付
			async submit() {
				if (this.isloading) {
					return
				}
				this.createLoading()
				// 全米券支付||实付款价格为0
				let order = [{
					goods: `${this.optionData.id},0,1`,
					"supplier_uid": "0",
					"is_checked_claim_receipt": 0,
					"claim_template_id": 0,
					"claim_receipt_price": "0.00",
					"claim_amount": "0.00",
					"id": `${this.optionData.id}`,
					"dispatchtype": 0,
					"fromcart": 0,
					"cartids": 0,
					"remark": this.remark || '',
					"deduct": 0,
					"deduct2": 0,
					"yunbi": 0,
					"freegoods": "0",
					"addressid": `${this.displayInfo.address?this.displayInfo.address.id:''}`,
					"groupsn": ""
				}]
				order = JSON.stringify(order)
				const res = await this.$axios('confirm/create', "POST", 'order', {
					order,
					rate: 0,
					type: 0,
					ismicredit: this.isMi && this.mpay > 0 ? 1 : 0,
					mpay: this.mpay || 0,
					agent_member_id: uni.getStorageSync('mid') || 0,
					date_card_id: this.optionData.pintuan_card_id || '',
					...this.optionData,
				}, 1)
				if (res.data.code != 200) {
					this.clearLoading(true, res.data.data)
					return
				}
				this.clearLoading()
				if (res.data.data.orderid == '') {
					return this.$api.msg('下单失败，请重试!')
				}
				this.$navRed(
					`/pages/money/pay?orderid=${res.data.data.orderid}&type=pintuan&agent_member_id=${uni.getStorageSync('mid') || 0}&pintuan_list_id=${this.optionData.pintuan_list_id||''}&leader_member_id=${this.optionData.leader_member_id||''}&date_card_id=${this.optionData.pintuan_card_id||''}`
				)
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-bottom: 240upx;
		padding-top: 20rpx;
	}

	.addresspop {
		background-color: #fdf8ff;
		padding: 20rpx;
		border-radius: 20rpx 20rpx 0 0;
		height: 900rpx;

		.tit {
			width: 100%;
			text-align: center;
			position: relative;
			font-weight: bold;
			font-size: 32rpx;

			image {
				width: 28rpx;
				height: 28rpx;
				position: absolute;
				right: 0;
				top: 4rpx;
				color: #5B5B5B;
			}
		}
	}

	.change_delivery {
		display: flex;
		padding: 20rpx;

		.c_d_item {
			color: $bg-color;
			background: #fff;
			text-align: center;
			width: 140rpx;
			line-height: 60rpx;
			border-radius: 10rpx 0 0 10rpx;
		}

		.c_d_item:nth-child(2) {
			border-radius: 0 10rpx 10rpx 0;
		}

		.c_d_active {
			background: $bg-color;
			color: #fff;
		}
	}

	.pickup {
		position: fixed;
		background: #fff;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		z-index: 999;

		.p_scroll {
			height: 100%;
			padding: 20rpx;

			.p_item {
				margin-bottom: 20rpx;
				padding-bottom: 60rpx;
				border-bottom: 1rpx solid #e6dcdc5c;

				.p_top {
					display: flex;
					align-items: center;
					justify-content: space-between;

					image {
						width: 120rpx;
						height: 120rpx;
						border-radius: 12rpx;
					}

					.p_info {
						flex: 1;
						padding-left: 20rpx;

						.p_tit {
							font-weight: bold;
							margin-bottom: 10rpx;
						}

						.p_address {
							font-size: 24rpx;
							color: #999;
						}
					}
				}

				.p_btn {
					width: 120rpx;
					height: 40rpx;
					border-radius: 33rpx;
					line-height: 40rpx;
					color: #fff;
					text-align: center;
					font-size: 22rpx;
					float: right;
					background: linear-gradient(270deg, #8917E2 0%, #B92DF1 100%);
				}
			}
		}
	}

	.address-section {
		padding: 30upx 0;
		background: #fff;
		position: relative;

		.order-content {
			display: flex;
			align-items: center;
		}

		.icon-shouhuodizhi {
			flex-shrink: 0;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 90upx;
			color: #888;
			font-size: 44upx;
		}

		.cen {
			display: flex;
			flex-direction: column;
			flex: 1;
			font-size: 28upx;
			color: $font-color-dark;
		}

		.name {
			font-size: 34upx;
			margin-right: 24upx;
		}

		.address {
			margin-top: 16upx;
			margin-right: 20upx;
			color: $font-color-light;
		}

		.icon-you {
			font-size: 32upx;
			color: $font-color-light;
			margin-right: 30upx;
		}

		.a-bg {
			position: absolute;
			left: 0;
			bottom: 0;
			display: block;
			width: 100%;
			height: 5upx;
		}
	}

	.goods-section {
		background: #fff;
		padding-bottom: 1px;

		.g-header {
			display: flex;
			align-items: center;
			height: 84upx;
			padding: 0 30upx;
			position: relative;
		}

		.logo {
			display: block;
			width: 50upx;
			height: 50upx;
			border-radius: 100px;
		}

		.name {
			font-size: 30upx;
			color: $font-color-base;
			margin-left: 24upx;
		}

		.g-item {
			display: flex;
			margin: 20upx 30upx;

			image {
				flex-shrink: 0;
				display: block;
				width: 140upx;
				height: 140upx;
				border-radius: 4upx;
			}

			.right {
				flex: 1;
				padding-left: 24upx;
				overflow: hidden;
			}

			.title {
				font-size: 30upx;
				color: $font-color-dark;
			}

			.spec {
				font-size: 26upx;
				color: $font-color-light;
			}

			.price-box {
				display: flex;
				align-items: center;
				font-size: 32upx;
				color: $font-color-dark;
				padding-top: 10upx;

				.price {
					margin-bottom: 4upx;
				}

				.number {
					font-size: 26upx;
					color: $font-color-base;
					margin-left: 20upx;
				}
			}

			.step-box {
				position: relative;
			}
		}
	}

	.yt-list {
		margin-top: 16upx;
		background: #fff;
	}

	.yt-list-cell {
		display: flex;
		align-items: center;
		padding: 10upx 30upx 10upx 40upx;
		line-height: 70upx;
		position: relative;

		&.cell-hover {
			background: #fafafa;
		}

		&.b-b:after {
			left: 30upx;
		}

		.cell-icon {
			height: 32upx;
			width: 32upx;
			font-size: 22upx;
			color: #fff;
			text-align: center;
			line-height: 32upx;
			background: #f85e52;
			border-radius: 4upx;
			margin-right: 12upx;

			&.hb {
				background: #ffaa0e;
			}

			&.lpk {
				background: #3ab54a;
			}

		}

		.cell-more {
			align-self: center;
			font-size: 24upx;
			color: $font-color-light;
			margin-left: 8upx;
			margin-right: -10upx;
		}

		.cell-tit {
			flex: 1;
			font-size: 26upx;
			color: $font-color-light;
			margin-right: 10upx;
		}

		.cell-tip {
			font-size: 26upx;
			color: $font-color-dark;

			&.disabled {
				color: $font-color-light;
			}

			&.active {
				color: $base-color;
			}

			&.red {
				color: #e10602;
			}
		}

		&.desc-cell {
			.cell-tit {
				max-width: 90upx;
			}
		}

		.desc {
			flex: 1;
			font-size: $font-base;
			color: $font-color-dark;
		}
	}

	.play-type {}

	/* 支付列表 */
	.shell_input {
		background: #FFFFFF;
		border-bottom: 1px hsl(0, 0%, 87%) solid;
		display: flex;
		align-items: center;
		padding: 0 30upx;
		position: fixed;
		bottom: 140upx;
		height: 90upx;
		width: 100%;
		z-index: 998;
	}

	.shell_input input {
		flex: 1;
		font-size: 26upx;
	}

	.shell_input text {
		color: #0062CC;
	}

	.footer {
		position: fixed;
		left: 0;
		bottom: 0;
		display: flex;
		align-items: center;
		width: 100%;
		height: 140upx;
		font-size: 30upx;
		background-color: #fff;
		z-index: 88;
		color: $font-color-base;

		.price-content {
			padding-left: 30upx;
			font-size: 22upx;
			flex: 1;
		}

		.price-tip {
			color: $base-color;
			margin-left: 8upx;
		}

		.price {
			font-size: 40upx;
			color: $base-color;
		}

		.submit {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 170upx;
			height: 100%;
			color: #fff;
			font-size: 28upx;
			background-color: $base-color;
		}

		.disabledSumbit {
			background-color: rgba($base-color, 0.4);
		}

		.unsubmit {
			background: #999;
			width: 280rpx;
			padding: 0 20rpx;
			text-align: center;
		}
	}

	/* 优惠券面板 */
	.mask {
		display: flex;
		align-items: flex-end;
		position: fixed;
		left: 0;
		top: var(--window-top);
		bottom: 0;
		width: 100%;
		background: rgba(0, 0, 0, 0);
		z-index: 9995;
		transition: .3s;

		.mask-content {
			width: 100%;
			min-height: 30vh;
			max-height: 70vh;
			background: #f3f3f3;
			transform: translateY(100%);
			transition: .3s;
			overflow-y: scroll;
		}

		&.none {
			display: none;
		}

		&.show {
			background: rgba(0, 0, 0, .4);

			.mask-content {
				transform: translateY(0);
			}
		}
	}

	/* 优惠券列表 */
	.coupon-item {
		display: flex;
		flex-direction: column;
		margin: 20upx 24upx;
		background: #fff;

		.con {
			display: flex;
			align-items: center;
			position: relative;
			height: 120upx;
			padding: 0 30upx;

			&:after {
				position: absolute;
				left: 0;
				bottom: 0;
				content: '';
				width: 100%;
				height: 0;
				border-bottom: 1px dashed #f3f3f3;
				transform: scaleY(50%);
			}
		}

		.left {
			display: flex;
			flex-direction: column;
			justify-content: center;
			flex: 1;
			overflow: hidden;
			height: 100upx;
		}

		.title {
			font-size: 32upx;
			color: $font-color-dark;
			margin-bottom: 10upx;
		}

		.time {
			font-size: 24upx;
			color: $font-color-light;
		}

		.right {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			font-size: 26upx;
			color: $font-color-base;
			height: 100upx;
		}

		.price {
			font-size: 44upx;
			color: $base-color;

			&:before {
				content: '￥';
				font-size: 34upx;
			}
		}

		.tips {
			font-size: 24upx;
			color: $font-color-light;
			line-height: 60upx;
			padding-left: 30upx;
		}

		.circle {
			position: absolute;
			left: -6upx;
			bottom: -10upx;
			z-index: 10;
			width: 20upx;
			height: 20upx;
			background: #f3f3f3;
			border-radius: 100px;

			&.r {
				left: auto;
				right: -6upx;
			}
		}
	}

	.c_info {
		padding: 20upx;
		box-sizing: border-box;
	}

	.pay_status {
		font-size: 24upx;
		display: flex;
		align-items: center;
		padding: 0 30upx;
	}
</style>